﻿using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using Microsoft.Phone.Tasks;

namespace MyMeetup.ViewModels
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class AlbumPageViewModel : ViewModelBase
    {
        private DataService _dataService;

        public RelayCommand<PhotoResult> PostPhotoCommand { get; private set; } 

        /// <summary>
        /// Initializes a new instance of the AlbumPageViewModel class.
        /// </summary>
        public AlbumPageViewModel()
        {
            PostPhotoCommand = new RelayCommand<PhotoResult>(PostPhoto);
            Messenger.Default.Register<PropertyChangedMessage<Album>>(this,
                                                                        (action) => UpdateAlbumPhotos(action.NewValue));
            _dataService = new DataService();
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real": Connect to service, etc...
            ////}
        }

        private void UpdateAlbumPhotos(Album newAlbum)
        {
            if (newAlbum == null) return;
            AlbumDetails = newAlbum;
            GetPhotos();
            
        }

        private void PostPhoto(PhotoResult photo)
        {
            int idx = photo.OriginalFileName.LastIndexOf('\\');
            string filename = photo.OriginalFileName.Substring(idx);

            _dataService.UploadPhotoToAlbum(AlbumDetails.photo_album_id, filename, photo.ChosenPhoto, GetPhoto, ResponseHelper.ErrorCallback);
        }


        private void GetPhotos()
        {
            _dataService.GetAlbumPhotos(AlbumDetails.photo_album_id, (x) => { DispatcherHelper.Initialize();
                                                                                DispatcherHelper.CheckBeginInvokeOnUI(
                                                                                    () => AlbumPhotos = x);
            }, ResponseHelper.ErrorCallback);
        }



        private void GetPhoto(long photoId)
        {
            _dataService.GetPhoto(photoId, (x) => AddPhotoToAlbum(x), ResponseHelper.ErrorCallback);
        }

        private void AddPhotoToAlbum(Photo photo)
        {
            AlbumPhotos.Insert(0,photo);
        }


        public Album AlbumDetails { get; private set; }

        /// <summary>
        /// The <see cref="AlbumPhotos" /> property's name.
        /// </summary>
        public const string AlbumPhotosPropertyName = "AlbumPhotos";

        private ObservableCollection<Photo> _albumPhotos;

        /// <summary>
        /// Gets the AlbumPhotos property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<Photo> AlbumPhotos
        {
            get
            {
                return _albumPhotos;
            }

            set
            {
                if (_albumPhotos == value)
                {
                    return;
                }

                var oldValue = _albumPhotos;
                _albumPhotos = value;

                // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
                RaisePropertyChanged(AlbumPhotosPropertyName, oldValue, value, true);
            }
        }
    }
}